﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace NRemedy.Test
{
    public interface IIPDataProvider
    {
        IEnumerable<AR.Entity.ASB_AdminIPConfig> GetIPs();
    }

    public class IPDataProviderImp : IIPDataProvider
    {
        private string ipfile = @"D:\Project\C#\NRemedy\NRemedy.Test\IP.txt";
        private char splitChar = '\t';
        private const int INDEX_IP = 0;
        private const int INDEX_SEGMENT = 1;
        private const int INDEX_GATEWAY = 2;
        private const int INDEX_MASK = 3;
        private const int INDEX_USED = 4;


        public IEnumerable<AR.Entity.ASB_AdminIPConfig> GetIPs()
        {
            List<AR.Entity.ASB_AdminIPConfig> IPs = new List<AR.Entity.ASB_AdminIPConfig>();
            Stream fs = null;
            try
            {
                fs = File.OpenRead(ipfile);
                StreamReader sr = new StreamReader(fs, Encoding.Default, true);
                string line = string.Empty;
                line = sr.ReadLine();

                while (!string.IsNullOrEmpty(line))
                {
                    string[] ipinfo = line.Split(splitChar);
                    if (ipinfo.Length == 5)
                    {
                        AR.Entity.ASB_AdminIPConfig ip = new AR.Entity.ASB_AdminIPConfig();
                        ip.cChr_IPAddress = ipinfo[INDEX_IP];
                        ip.cChr_Gateway = ipinfo[INDEX_GATEWAY];
                        ip.cChr_Mask = ipinfo[INDEX_MASK];
                        ip.cInt_IsUsed = int.Parse(ipinfo[INDEX_USED]);
                        ip.cChr_Segment = ipinfo[INDEX_SEGMENT];
                        IPs.Add(ip);
                    }
                    line = sr.ReadLine();
                }
                //add an empty

                AR.Entity.ASB_AdminIPConfig ip2 = new AR.Entity.ASB_AdminIPConfig();
                ip2.cChr_IPAddress = null;
                ip2.cChr_Gateway = null;
                ip2.cChr_Mask = null;
                ip2.cInt_IsUsed = 0;
                ip2.cChr_Segment = null;
                IPs.Add(ip2);

            }
            catch(Exception e)
            {
                throw e;
            }
            finally
            {
                if(fs != null)
                    fs.Close();
            }
            return IPs.ToArray();
        }
    }
}
